<?php
Junction_FileCabinet::using("Junction_Core_Mapping");
Junction_FileCabinet::using("Junction_Core_Property");
Junction_FileCabinet::using("Test_Mock_Client");

class Test_Junction_Core_Mapping extends UnitTestCase {
	
	public function testConstruct() {
		$mapping = new Junction_Core_Mapping("User", "Users");
	}
	
	public function testBinding() {
		$mapping = new Junction_Core_Mapping("User", "Users");
		$mapping->bindKey(new Junction_Core_Property("foo", "bar"));
		$mapping->bindProperty(new Junction_Core_Property("foo", "bar"));
	}
	
	public function testInsert() {
		$mapping = new Junction_Core_Mapping("User", "users");
		$mapping->bindKey(new Junction_Core_Property("Id", "id"));
		$mapping->bindProperty(new Junction_Core_Property("Email", "email"));
		$mapping->bindProperty(new Junction_Core_Property("Date", "date_added"));
		$mapping->bindProperty(new Junction_Core_Property("Password", "password"));
		
		$user = new Test_Mock_Client();
		$user->setEmail("foo@bar.com");
		$user->setPassword("password");
		$user->setDate(time());
		
		$this->assertTrue($mapping->save($user) === true);
		$this->assertTrue($user->getId() > 0);
	}
	
	public function testUpdate() {
		$mapping = new Junction_Core_Mapping("User", "users");
		$mapping->bindKey(new Junction_Core_Property("Id", "id"));
		$mapping->bindProperty(new Junction_Core_Property("Email", "email"));
		$mapping->bindProperty(new Junction_Core_Property("Date", "date_added"));
		$mapping->bindProperty(new Junction_Core_Property("Password", "password"));
		
		$user = new Test_Mock_Client();
		$user->setEmail("foo@bar.com");
		$user->setPassword("password");
		$user->setDate(time());

		$mapping->save($user);
		
		$user->setPassword("anewpassword");
		
		$this->assertTrue($mapping->save($user) === true);
	}
	
	public function testDelete() {
		$mapping = new Junction_Core_Mapping("User", "users");
		$mapping->bindKey(new Junction_Core_Property("Id", "id"));
		$mapping->bindProperty(new Junction_Core_Property("Email", "email"));
		$mapping->bindProperty(new Junction_Core_Property("Date", "date_added"));
		$mapping->bindProperty(new Junction_Core_Property("Password", "password"));
		
		$user = new Test_Mock_Client();
		$user->setEmail("foo@bar.com");
		$user->setPassword("password");
		$user->setDate(time());

		$mapping->save($user);
		
		$this->assertTrue($mapping->delete(" WHERE id = " . $user->getId()));
	}
	
	public function testFetch() {
		$mapping = new Junction_Core_Mapping("Test_Mock_Client", "users");
		$mapping->bindKey(new Junction_Core_Property("Id", "id"));
		$mapping->bindProperty(new Junction_Core_Property("Email", "email"));
		$mapping->bindProperty(new Junction_Core_Property("Date", "date_added"));
		$mapping->bindProperty(new Junction_Core_Property("Password", "password"));
		
		$user = new Test_Mock_Client();
		$user->setEmail("foo@bar.com");
		$user->setPassword("password");
		$user->setDate(time());

		$mapping->save($user);
		
		$users = $mapping->fetch(" WHERE id = " . $user->getId());
		$this->assertTrue(is_a($users->current(), "Test_Mock_Client"));
		
		$users = $mapping->fetch(" WHERE email = '" . $user->getEmail() . "'");
		$users->next();
		$this->assertTrue(is_a($users->current(), "Test_Mock_Client"));
	}
	
	public function testIterate() {
		$mapping = new Junction_Core_Mapping("User", "users");
		$mapping->bindKey(new Junction_Core_Property("Id", "id"));
		$mapping->bindProperty(new Junction_Core_Property("Email", "email"));
		$mapping->bindProperty(new Junction_Core_Property("Date", "date_added"));
		$mapping->bindProperty(new Junction_Core_Property("Password", "password"));
		
		$props = array();
		foreach ($mapping as $property) {
			$props[] = $property;
		}
		$this->assertTrue(count($props) == 4);
		
		$props2 = array();
		foreach ($mapping as $property) {
			$props2[] = $property;
		}
		$this->assertTrue(count($props2) == 4);
		$this->assertTrue($props == $props2);
	}
}
?>